/* model */
import { BaseSelectUserVuexPropertyEnum } from '@src/component/common/BaseSelectUser/store'
import { SelectUserComponentServiceProvider } from '@src/component/common/BaseSelectUser/model'
import MsgModel from '@model/MsgModel'
/* store */
import { useBaseSelectUserStore } from '@src/component/common/BaseSelectUser/store/hooks/base'
/* vue */
import { computed } from 'vue'
/* type */
import { 
  BaseSelectUserGetServiceProviderListModel,
  BaseSelectUserGetServiceProviderListResult, 
  BaseSelectUserGetServiceProviderQualificationListResult, 
  BaseSelectUserGetServiceProviderTypeListResult, 
  BaseSelectUserGetServiceProviderUserListModel, 
  BaseSelectUserGetServiceProviderUserListResult, 
  SelectUserServiceProviderQualificationType, 
  SelectUserServiceProviderTypeType 
} from '@src/component/common/BaseSelectUser/types'
/* util */
import { isEmpty, isNotEmpty } from '@src/util/type'

/** 
 * @description 选人控件 服务商列表
*/
function useBaseSelectUserStoreServiceProviderList() {
  
  const store = useBaseSelectUserStore()
  const { currentServiceProviderNav } = useBaseSelectUserStoreCurrentServiceProviderNav()
  
  // 服务商列表
  const serviceProviderList = computed(() => store.state.serviceProviderList)
  
  // 子服务商列表
  const serviceProviderChildList = computed(() => {
    
    if (isEmpty(currentServiceProviderNav.value)) {
      return []
    }
    
    return currentServiceProviderNav.value?.tagChildren || []
  })
  
  // 服务商列表是否为空
  const isServiceProviderListEmpty = computed(() => {
    return isEmpty(serviceProviderList.value)
  })
  
  // 服务商列表是否不为空
  const isServiceProviderListNotEmpty = computed(() => {
    return isNotEmpty(serviceProviderList.value)
  })
  
  // 子服务商列表是否为空
  const isServiceProviderChildListEmpty = computed(() => {
    return isEmpty(serviceProviderChildList.value)
  })
  
  const setServiceProviderList = (value: SelectUserComponentServiceProvider[] = []) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetServiceProviderList, value)
  }
  
  return {
    isServiceProviderListEmpty,
    isServiceProviderListNotEmpty,
    isServiceProviderChildListEmpty,
    serviceProviderList,
    serviceProviderChildList,
    setServiceProviderList
  }
}

/** 
 * @description 选人控件 服务商资质列表
*/
function useBaseSelectUserStoreServiceProviderQualificationList() {
  
  const store = useBaseSelectUserStore()
  
  const serviceProviderQualificationList = computed(() => store.state.serviceProviderQualificationList)
  
  const setServiceProviderQualificationList = (value: SelectUserServiceProviderQualificationType[]) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetServiceProviderQualificationList, value)
  }
  
  return {
    serviceProviderQualificationList,
    setServiceProviderQualificationList
  }
}

/** 
 * @description 选人控件 服务商类型列表
*/
function useBaseSelectUserStoreServiceProviderTypeList() {
  
  const store = useBaseSelectUserStore()
  
  const serviceProviderTypeList = computed(() => store.state.serviceProviderTypeList)
  
  const setServiceProviderTypeList = (value: SelectUserServiceProviderTypeType[]) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetServiceProviderTypeList, value)
  }
  
  return {
    serviceProviderTypeList,
    setServiceProviderTypeList
  }
}

/** 
 * @description 选人控件 当前查看展开的服务商
*/
function useBaseSelectUserStoreCurrentToggleServiceProvider() {
  
  const store = useBaseSelectUserStore()
  
  // 当前查看展开的服务商
  const currentToggleServiceProvider = computed(() => store.state.currentToggleServiceProvider)
  
  // 当前查看展开的服务商是否不为空
  const isCurrentToggleServiceProviderNotEmpty = computed(() => {
    return isNotEmpty(currentToggleServiceProvider.value)
  })
  
  // 当前查看展开的服务商是否为空
  const isCurrentToggleServiceProviderEmpty = computed(() => {
    return isEmpty(currentToggleServiceProvider.value)
  })
  
  const setCurrentToggleServiceProvider = (value: SelectUserComponentServiceProvider | null = null) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetCurrentToggleServiceProvider, value)
  }
  
  return {
    currentToggleServiceProvider,
    isCurrentToggleServiceProviderEmpty,
    isCurrentToggleServiceProviderNotEmpty,
    setCurrentToggleServiceProvider
  }
}

/** 
 * @description 选人控件 服务商导航列表
*/
function useBaseSelectUserStoreServiceProviderNavList() {
  
  const store = useBaseSelectUserStore()
  
  const serviceProviderNavList = computed(() => store.state.serviceProviderNavList)
  
  const isServiceProviderNavListEmpty = computed(() => isEmpty(serviceProviderNavList.value))
  const isServiceProviderNavListNotEmpty = computed(() => isNotEmpty(serviceProviderNavList.value))
  
  const setServiceProviderNavList = (value: SelectUserComponentServiceProvider[] = []) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetServiceProviderNavList, value)
  }
  
  return {
    serviceProviderNavList,
    isServiceProviderNavListEmpty,
    isServiceProviderNavListNotEmpty,
    setServiceProviderNavList
  }
}

/** 
 * @description 选人控件 当前服务商导航
*/
function useBaseSelectUserStoreCurrentServiceProviderNav() {
  
  const store = useBaseSelectUserStore()
  
  const currentServiceProviderNav = computed(() => store.state.currentServiceProviderNav)
  
  const isCurrentServiceProviderNavEmpty = computed(() => isEmpty(currentServiceProviderNav.value))
  const isCurrentServiceProviderNavNotEmpty = computed(() => isNotEmpty(currentServiceProviderNav.value))
  
  const setCurrentServiceProviderNav = (value: SelectUserComponentServiceProvider | null = null) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetCurrentServiceProviderNav, value)
  }
  
  return {
    currentServiceProviderNav,
    isCurrentServiceProviderNavEmpty,
    isCurrentServiceProviderNavNotEmpty,
    setCurrentServiceProviderNav
  }
}

/** 
 * @description 查询选人控件的服务商列表
*/
function useBaseSelectUserStoreFetchServiceProviderList() {
  
  const store = useBaseSelectUserStore()
  
  function fetchServiceProviderList(params: BaseSelectUserGetServiceProviderListModel): Promise<MsgModel<BaseSelectUserGetServiceProviderListResult>> {
    return (
      store.dispatch(BaseSelectUserVuexPropertyEnum.FetchServiceProviderList, params).then(result => {
        return result
      })
    )
  }
  
  return fetchServiceProviderList
}

/** 
 * @description 查询选人控件的服务商资质列表
*/
function useBaseSelectUserStoreFetchServiceProviderQualificationList() {
  
  const store = useBaseSelectUserStore()
  
  function fetchServiceProviderQualificationList(): Promise<MsgModel<BaseSelectUserGetServiceProviderQualificationListResult>> {
    return (
      store.dispatch(BaseSelectUserVuexPropertyEnum.FetchServiceProviderQualificationList).then(result => {
        return result
      })
    )
  }
  
  return fetchServiceProviderQualificationList
}

/** 
 * @description 查询选人控件 服务商类型数据
*/
function useBaseSelectUserStoreFetchServiceProviderTypeList() {
  
  const store = useBaseSelectUserStore()
  
  function fetchServiceProviderTypeList(): Promise<MsgModel<BaseSelectUserGetServiceProviderTypeListResult>> {
    return (
      store.dispatch(BaseSelectUserVuexPropertyEnum.FetchServiceProviderTypeList).then(result => {
        return result
      })
    )
  }
  
  return fetchServiceProviderTypeList
}

/** 
 * @description 查询选人控件 服务商下 人员列表
*/
function useBaseSelectUserStoreFetchServiceProviderUserList() {
  
  const store = useBaseSelectUserStore()
  
  function fetchServiceProviderUserList(params: BaseSelectUserGetServiceProviderUserListModel): Promise<MsgModel<BaseSelectUserGetServiceProviderUserListResult>> {
    return (
      store.dispatch(BaseSelectUserVuexPropertyEnum.FetchServiceProviderUserList, params).then(result => {
        return result
      })
    )
  }
  
  return fetchServiceProviderUserList
}

export {
  useBaseSelectUserStoreServiceProviderList,
  useBaseSelectUserStoreServiceProviderQualificationList,
  useBaseSelectUserStoreServiceProviderTypeList,
  useBaseSelectUserStoreFetchServiceProviderList,
  useBaseSelectUserStoreFetchServiceProviderQualificationList,
  useBaseSelectUserStoreFetchServiceProviderUserList,
  useBaseSelectUserStoreFetchServiceProviderTypeList,
  useBaseSelectUserStoreCurrentToggleServiceProvider,
  useBaseSelectUserStoreServiceProviderNavList,
  useBaseSelectUserStoreCurrentServiceProviderNav
}
